"""author: wenyao
   data: 2021/4/19
   project: devopscmdb
"""

#格式  对符合某种规律的字符串的匹配和验证
#xxx@xx.xx

#正则表达式
#过滤查找指定字符
#验证字符是否符合要求

import re

##############查找匹配##############
#1、search  从头开始查找，返回匹配到的第一个字符串
#2、match   从开头进行匹配，返回匹配到的第一个字符串，如果开头没有匹配，就查找失败
#           匹配开头

# >>> import re
# >>> re.search("123","hello world 123 456 python")
# <_sre.SRE_Match object; span=(12, 15), match='123'>
# >>> ret = re.search("123","hello world 123 456 python")
# >>> ret
# <_sre.SRE_Match object; span=(12, 15), match='123'>
# >>> ret = re.search("123","hello world  456 python")
# >>> ret
# >>> ret = re.search("123","hello 123 world 123  python")
# >>> ret
# <_sre.SRE_Match object; span=(6, 9), match='123'>
# >>> ret = re.match("123","hello 123 world 123  python")
# >>> ret
# >>> ret = re.match("123","123 hello 123 world 123  python")
# >>> ret
# <_sre.SRE_Match object; span=(0, 3), match='123'>

#################原始字符串##########
# >>> print("hello\tsanchuang")
# hello	sanchuang
# >>> print(r"hello\tsanchuang")
# hello\tsanchuang

# >>> re.search("hello\tworld","sanchuang hello\tworld")
# <_sre.SRE_Match object; span=(10, 21), match='hello\tworld'>
# >>> re.search("hello\\tworld","sanchuang hello\\tworld")
# >>> re.search("hello\\\tworld","sanchuang hello\\\tworld")
# >>> re.search(r"hello\\\tworld","sanchuang hello\\\tworld")
# <_sre.SRE_Match object; span=(10, 22), match='hello\\\tworld'>


####match对象#########
# >>> ret = re.search(r"cat","dog cat cat cat")
# >>> ret
# <_sre.SRE_Match object; span=(4, 7), match='cat'>
# >>> ret.groups()
# ()
# >>> ret.group()
# 'cat'
# >>> ret.start()
# 4
# >>> ret.end()
# 7

####findall 或者finditer
# msg = "It's raning cats and dogs, cat1cat2"
# result = re.findall("cat",msg)
# print(result)
# #返回的是一个迭代器
# result = re.finditer("cat",msg)
# print(result)
# for i in result:
#     print(i.group())

###正则替换
#re.sub("匹配正则","替换的内容", string)
# msg = "i am learning python python2 python3"
# print(re.sub("python","PYTHON",msg))

##正则编译compile
# msg = "welcome to sanchuang!"
# msg2 = "welcome to hunan!"
# msg3 = "welcome to changsha!"
# #"welcome" in msg
# #
# result = re.findall("welcome",msg)
# result1 = re.findall("welcome",msg2)
# result2 = re.findall("welcome",msg3)
#
# reg = re.compile("welcome")
# print(reg.findall(msg))
# print(reg.findall(msg2))
# print(reg.findall(msg3))


##################正则表达式##########
#区间  []  根据ascii码
# ret = re.findall(r"[12pP]y","123 1y422ypy Python")
# print(ret)
# ret = re.findall(r"[0-9]","a12 34b zz1")
# print(ret)
# ret = re.findall(r"[a-z]","aPythON")
# print(ret)
# ret = re.findall(r"[A-Z]","aPythON")
# print(ret)
# ret = re.findall(r"[A-z]","aPythON")
# print(ret)
# ret = re.findall(r"[A-Za-z0-9_]","aPythON_134")
# print(ret)

#区间取反
# ret = re.findall(r"[^A-Z][0-9]","abc123$PYxyz")
# print(ret)

#匹配或
# msg = "welcome to changsha , welcome to hunan"
# ret = re.search("changsha|hunan",msg)
# print(ret.group())
# ret = re.findall("changsha|hunan",msg)
# print(ret)

# . 占位符，表示任意字符（除\n以外）
# ret = re.findall("p.thon","PythonppthonpTHON p thon p-thon")
# print(ret)

#快捷标识
# \A 匹配字符串开始
# \b word\b词边界
# \w 匹配包括下划线的任何单词字符。等价于'[A-Za-z0-9_]'
# \W 匹配任何非单词字符。等价于'[^A-Za-z0-9_]'
# \d 匹配一个数字字符。等价于[0-9]
# \D 匹配一个非数字字符。等价于[^0-9]
# \s 匹配任何空白字符，包括空格、制表符、换页符等等。等价于[\f\n\r\t\v]
# \S 匹配任何非空白字符。等价于[^\f\n\r\t\v]
# ret = re.findall(r"\Ahello","hello nihao, hello world")
# print(ret)
#
# ret = re.findall(r"\bworld","helloworld, world 123world world123 #world#")
# print(ret)
#
# ret = re.findall(r"\bworld\b","helloworld, world 123world world123 #world#")
# print(ret)
# ret = re.findall(r"\Bworld\B","helloworld, world 123world world123 #world# abcworldabc")
# print(ret)
#
# ret = re.findall(r"\w","lswe@#rws0.xow34r")
# print(ret)

#匹配开始与结束
#^, $
# ret = re.findall(r"^python","python,hello,python")
# print(ret)
# ret = re.findall(r"python$","python,hello,python")
# print(ret)

###########正则重复
#通配符： *  ? +
#? 表示匹配前一项0次或1次
# ret = re.findall("py?","pytpspyy")
# print(ret)
# #+ 表示匹配前一项1-n次
# ret = re.findall("py+","pytpspyy")
# print(ret)
# #* 表示匹配前一项任意次  0-n次
# ret = re.findall("py*","pytpspyy")
# print(ret)
# #{n,m} 匹配n到m次
# ret = re.findall("py{1,3}","pytpyyypyy")
# print(ret)
# #{n} 指定匹配n次
# ret = re.findall("py{3}","pytpyyypyy")
# print(ret)

#
# while 1:
#     num = input("请输入一个数字:")
# #+100 100 0100 +0100
#     if re.findall(r"^\+?0*[1-9]\d*$",num):
#         print("是正整数")
#     elif re.findall(r"^-[1-9]\d*$",num):
#         print("是负整数")
#     # elif re.findall(r"^[+-]?\d+\.\d+$",num):
#     elif re.findall(r"^[+-]?0\.\d+$|^[+-]?[1-9]\d*\.\d+$",num):
#         print("是浮点数")

#########贪婪与非贪婪####
#贪婪 尽可能的匹配长的字符串
#非贪婪 匹配到了就退出（+?  *? {2,4}?）
# msg = "cats and dogs, cats1 and dog1"
# print(re.findall(r"cat.*?s",msg))

###############正则分组
#group 默认参数0 表示输出匹配的整个字符串
#      参数n(n>0)表示 输出第n个分组字符串
# msg = "tel:173-7572-2991"
# ret = re.search(r'(\d{3})-(\d{4})-(\d{4})',msg)
# print(ret.group())
# print(ret.group(0))
# print(ret.group(1))
# print(ret.group(2))
# print(ret.group(3))
# print(ret.group(4))

#分组向后引用
# msg = "cats dogs,cats dogs"
# print(re.search(r"(\w{4}) (\w{4}),\1 \2",msg).group())

#捕获分组
#分组之后匹配到的数据会放在内存里，并且给定一个从1开始的索引
#所以，捕获分组可以进行分组向后引用

#非捕获分组(?:)
#只分组不捕获， 不放在内存里，不能使用分组向后引用
# msg = "cats dogs,dogs"
# print(re.search(r"(?:\w{4})\s(\w{4}),\1",msg).group())

#如果有捕获分组，findall只会匹配捕获分组内容
# msg = "tel:173-7572-2991"
# print(re.findall(r'(?:\d{3})-(\d{4})-(\d{4})',msg))

# msg = "yyy@126.comaaa@bb.comcom@126.comdd@qq.comxxxxxxx@163.com"
# #过滤出 126.com 163.com qq.com
# print(re.findall(r'(?:com)?(\w+@(?:qq|126|163)\.com)',msg))

#长度为8-10的用户密码（以字母开头、数字、下划线）
# print(re.findall(r"[A-Za-z]\w{7,9}","a123_aw3fs"))

#url  属于url格式，提取出url中的域名  域名（数字字母中划线）
#http://hu.www.baidu.com
#https://www.baidu.com
#http://www.baidu.com/xxxx
#http://baidu.com?
# print(re.findall(r"https?://((?:[a-zA-Z0-9-]+\.)+com)[/?]?","http://www.baidu.com/"))

#提取电话号码
#+数字出现2-3次 1开头11位
# print(re.findall(r"(?:\+\d{2,3})?1\d{10}","+9812345678901"))
#
# #匹配邮箱地址
# print(re.findall(r"\w+@\w+\.com","123@qq.com"))

msg = "<html>test1<div>test2<a href=/lianxi/>test3</a>test4<br>test5</div></html>"
#print(re.findall(r"<div>(?:(.*?)<.*?>(.*?)</.*?>(.*?))</div>",msg))
#print(re.findall(r"<div>(?:(.*)(?:<.*?>)?)+</div>",msg))
#print(re.findall(r"<div>(?:(?:<.*?>)?(\w+)(?:<.*?>)?)+</div>",msg))
#print(re.findall(r"<div>((.*?)(?<=>)(.*?)(?=<)?)</div>",msg))
# print(re.findall(r"",msg))

#命名分组
#
# msg = "cats and dogs"
# ret = re.search(r"(?P<animal>\w{4})\s(\w{3})",msg)
# print(ret.group())
# print(ret.groups())
# print(ret.groupdict())


# #正则标记
# msg = """
# hello
# python
# hello
# PYTHON
# """
# #大小写不明感标记
# ret = re.findall("python", msg, re.I)
# print(ret)
#
# #多行模式，更改^与$的行为
# ret = re.findall("^python$",msg,re.I|re.M)
# print(ret)
#
#
# ret = re.findall(r".+",msg)
# print(ret)

#re.S 使.表示任意字符，包括换行符
# ret = re.findall(r".+",msg,re.S)
# print(ret)
#
# #内联标记
# ret = re.findall("(?im)^python$",msg)
# print(ret)

#正则断言
#零宽断言
#找位置
#位置在右边的(?=) (?!)
#位置在左边的(?<=) (?<!)

# s = "a regular rbgular expression"
# print(re.findall(r"\w+(?=gular)",s))
# print(re.findall(r"re(?!gular)",s))
# print(re.findall(r"(?<=re)gular",s))
# print(re.findall(r"(?<!re)gular",s))

# msg = "i am cici, welcom to 'sc tl' ths"
# print(re.findall(r"\w+",msg))
# print(re.findall(r"(?<=\').+(?=\')|\w+",msg))

#爬取三创官网图片
#https://www.sanchuangedu.cn/
#img标签 src属性里的内容
#src的内容以/  完整的图片的路径：https://www.sanchuangede.cn + 你得到的src内容
#src="/upload/links/momo.jpg" --》https://www.sanchuangede.cn/upload/links/momo.jpg
#得到图片的连接地址，然后下载到本地
# import requests
# result = requests.get("https://www.sanchuangedu.cn/")
# print(result.text)



